package com.gc;

/**
 * Created by Lxk on 2019/2/24.
 */

class ThreadLocalUtil {
    public static ThreadLocal<String> local = new ThreadLocal<>();
}

class ThreadA extends Thread {

    public void run(){
        try{
            for(int i = 0;i <= 10; i++){
                ThreadLocalUtil.local.set("threadA" + i);
                System.out.println(ThreadLocalUtil.local.get());
                Thread.sleep(100);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

class ThreadB extends Thread {

    public void run(){
        try{
            for(int i = 0;i <= 10; i++){
                ThreadLocalUtil.local.set("ThreadB" + i);
                System.out.println(ThreadLocalUtil.local.get());
                Thread.sleep(100);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}

public class ThreadLocalTest {

    public static void main(String[] args){
        try{
            ThreadA threadA = new ThreadA();
            ThreadB threadB = new ThreadB();
            threadA.start();
            threadB.start();
            for(int i = 0;i <= 10; i++){
                ThreadLocalUtil.local.set("main" + i);
                System.out.println(ThreadLocalUtil.local.get());
                Thread.sleep(100);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
